import { createWebHashHistory, createRouter } from 'vue-router';  
import Home from '@/pages/home/home.vue';  
import Login from '@/pages/login/login.vue';  
import AlterPassword from '@/pages/home/alterPassword.vue';  
import Info from '@/pages/home/info.vue';
import Admin from '@/pages/layout/admin.vue';  
import UserManagement from '@/pages/layout/views/User/UserManagement.vue'; 
import DictionaryManagement from '@/pages/layout/views/Dictionary/DictionaryManagement.vue'; 
import AssessmentManagement from '@/pages/layout/views/Assessment/AssessmentManagement.vue'; 
import EditWord from '@/pages/layout/views/Dictionary/EditWord.vue'
import QuestionBank from '@/pages/layout/views/Assessment/QuestionBank.vue'
import Register from '@/pages/login/register.vue';


import NotFound from '@/pages/404.vue';  

const routes = [  
    {  
        path: '/',  
        component: Admin,
        children: [
            {  
                path: '/',  
                name: 'Home',
                component: Home,  
            },
            {
                path: '/user-management',
                name: 'UserManagement',
                component: UserManagement
            },
            {
                path: '/dictionary-management',
                name: 'DictionaryManagement',
                component: DictionaryManagement
            },
            {
                path: '/assessment-management',
                name: 'AssessmentManagement',
                component: AssessmentManagement,
            },
            {
                path: '/dictionary/edit-word',
                name: 'EditWord',
                component: EditWord,
            },
            {
                path: '/question-bank',
                name: 'QuestionBank',
                component: QuestionBank,
            },
            {
                path: '/info',
                name: 'Info',
                component: Info
            },
            {
                path: '/alter-password',
                name: 'AlterPassword',
                component: AlterPassword
            },
            {
                path: '/article-management',
                name: 'ArticleManagement',
                component: () => import('@/pages/layout/views/Article/ArticleManagement.vue'),
                meta: {
                    requiresAuth: true
                }
            }
        ]
    },  
    {  
        path: '/login',  
        name: 'Login',
        component: Login  
    }, 
    {  
        path: '/register',  
        name: 'Register',
        component: Register 
    },  
    {   
        path: '/:pathMatch(.*)*',  
        name: 'NotFound',   
        component: NotFound   
    }  
];   

const router = createRouter({  
  history: createWebHashHistory(),   
  routes,  
});  

// 添加全局前置守卫
router.beforeEach((to, from, next) => {
    // 如果访问的不是登录页或注册页，且未登录，则重定向到登录页
    if (!localStorage.getItem('token') && 
        to.path !== '/login' && 
        to.path !== '/register') {
        next('/login');
    } else {
        next();
    }
});

export default router;